part of 'page.dart';

class _IntroPageView extends StatelessWidget with ScreenUtilMixin {
  const _IntroPageView({
    required _IntroPageWloc wloc,
  }) : _wloc = wloc;

  final _IntroPageWloc _wloc;

  Widget _buildContent({
    required BuildContext context,
  }) {
    return Container(
      width: double.infinity,
      color: IntroPageTheme.of(context).backgroundColor,
      child: Column(
        children: [
          _buildImageBlock(
            context: context,
          ),
          Expanded(
            child: _Slider(
              tabBuilder: (index) {
                return _tabBuilder(
                  context: context,
                  index: index,
                );
              },
              dotCount: _wloc.tabController.length,
              tabController: _wloc.tabController,
              nextButtonTitle: IntroPageTranslation.of(context).nextButtonTitle,
              finishButtonText: IntroPageTranslation.of(context).finishButtonTitle,
              onFinishButtonPressed: _wloc.onFinishButtonPressed,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildImageBlock({
    required BuildContext context,
  }) {
    final effectiveImageHeight = 250 * logWidthScaleFactor;
    final effectiveImageFloorHeight = 65 * logWidthScaleFactor;
    final imageFloorOverlappingHeight = effectiveImageFloorHeight * 0.6;

    return Stack(
      children: [
        Column(
          children: [
            Container(
              width: double.infinity,
              color: IntroPageTheme.of(context).imageBackgroundColor,
              child: SafeArea(
                left: false,
                top: true,
                right: false,
                bottom: false,
                maintainBottomViewPadding: true,
                child: SizedBox(
                  height: effectiveImageHeight +
                      effectiveImageFloorHeight / 2 -
                      imageFloorOverlappingHeight,
                ),
              ),
            ),
            SizedBox(
              height: effectiveImageFloorHeight / 2,
            ),
          ],
        ),
        Positioned.fill(
          bottom: 0,
          child: Align(
            alignment: Alignment.bottomCenter,
            child: _ImageFloor(
              color: IntroPageTheme.of(context).imageFloorColor,
              height: effectiveImageFloorHeight,
            ),
          ),
        ),
        Positioned.fill(
          bottom: effectiveImageFloorHeight - imageFloorOverlappingHeight,
          child: Align(
            alignment: Alignment.bottomCenter,
            child: SizedBox(
              height: effectiveImageHeight,
              child: Image.asset(
                'assets/images/intro_image.png',
                height: effectiveImageHeight,
                fit: BoxFit.cover,
                filterQuality: FilterQuality.medium,
              ),
            ),
          ),
        ),
      ],
    );
  }

  Widget _tabBuilder({
    required BuildContext context,
    required int index,
  }) {
    if (index == 0) {
      return _buildWelcomeTab(
        context: context,
      );
    }

    if (index == 1) {
      return _buildFinishTab(
        context: context,
      );
    }

    throw UnimplementedError();
  }

  Widget _buildWelcomeTab({
    required BuildContext context,
  }) {
    return Column(
      children: [
        SizedBox(
          height: 80 * heightScaleFactor,
        ),
        Padding(
          padding: EdgeInsets.symmetric(
            horizontal: 30 * logWidthScaleFactor,
          ),
          child: ConstrainedBox(
            constraints: const BoxConstraints(
              maxWidth: 600,
            ),
            child: Text(
              IntroPageTranslation.of(context).welcomeTabHeading,
              style: IntroPageTheme.of(context).textTheme.tabHeading,
              textAlign: TextAlign.center,
              textScaleFactor: logWidthScaleFactor,
            ),
          ),
        ),
        const Spacer(),
      ],
    );
  }

  Widget _buildFinishTab({
    required BuildContext context,
  }) {
    return Column(
      children: [
        SizedBox(
          height: 40 * heightScaleFactor,
        ),
        Padding(
          padding: EdgeInsets.symmetric(
            horizontal: 30 * logWidthScaleFactor,
          ),
          child: ConstrainedBox(
            constraints: const BoxConstraints(
              maxWidth: 600,
            ),
            child: Text(
              IntroPageTranslation.of(context).algorithmTabHeading,
              style: IntroPageTheme.of(context).textTheme.tabHeading,
              textAlign: TextAlign.center,
              textScaleFactor: logWidthScaleFactor,
            ),
          ),
        ),
        SizedBox(
          height: 25 * heightScaleFactor,
        ),
        Padding(
          padding: EdgeInsets.symmetric(
            horizontal: 30 * logWidthScaleFactor,
          ),
          child: ConstrainedBox(
            constraints: const BoxConstraints(
              maxWidth: 600,
            ),
            child: Text(
              IntroPageTranslation.of(context).algorithmTabSubheading,
              style: IntroPageTheme.of(context).textTheme.tabSubheading,
              textAlign: TextAlign.center,
              textScaleFactor: logWidthScaleFactor,
            ),
          ),
        ),
        const Spacer(),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: CoreTheme.of(context).brightness == Brightness.light
          ? SystemUiOverlayStyle.dark
          : SystemUiOverlayStyle.light,
      child: Container(
        width: double.infinity,
        height: double.infinity,
        color: IntroPageTheme.of(context).backgroundColor,
        child: _buildContent(
          context: context,
        ),
      ),
    );
  }
}
